package guda.shop.common.developer;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;

public class ModuleGenerator {
    public static final String SPT = File.separator;
    public static final String ENCODING = "UTF-8";
    private static final Logger log = LoggerFactory.getLogger(ModuleGenerator.class);
    private Properties entityMap = new Properties();
    private String packageName;
    private String fileName;
    private File daoImplFile;
    private File daoFile;
    private File managerFile;
    private File managerImplFile;
    private File actionFile;
    private File pageListFile;
    private File pageEditFile;
    private File pageAddFile;
    private File daoImpl;
    private File dao;
    private File manager;
    private File managerImpl;
    private File action;
    private File pageList;
    private File pageEdit;
    private File pageAdd;

    public ModuleGenerator(String paramString1, String paramString2) {
        this.packageName = paramString1;
        this.fileName = paramString2;
    }

    private static void writeFile(File paramFile, String paramString)
            throws IOException {
        FileUtils.writeStringToFile(paramFile, paramString, "UTF-8");
    }

    public static void main(String[] paramArrayOfString) {
        String str1 = "guda.common.developer.template";
        String str2 = "template.properties";
        new ModuleGenerator(str1, str2).generate();
    }

    private void init() {
        try {
            log.debug("packName=" + this.packageName);
            log.debug("fileName=" + this.fileName);
            FileInputStream localFileInputStream = new FileInputStream(find(this.packageName, this.fileName));
            this.entityMap.load(localFileInputStream);
            String str1 = this.entityMap.getProperty("Entity");
            log.debug("entityUp:" + str1);
            if ((str1 == null) || (str1.trim().equals(""))) {
                log.warn("Entity not specified, exit!");
                return;
            }
            String str2 = str1.substring(0, 1).toLowerCase() + str1.substring(1);
            log.debug("entityLow:" + str2);
            this.entityMap.put("entity", str2);
            if (log.isDebugEnabled()) {
                Set localSet = this.entityMap.keySet();
                Iterator localIterator = localSet.iterator();
                while (localIterator.hasNext()) {
                    Object localObject = localIterator.next();
                    log.debug(localObject + "=" + this.entityMap.get(localObject));
                }
            }
        } catch (FileNotFoundException localFileNotFoundException) {
            localFileNotFoundException.printStackTrace();
        } catch (IOException localIOException) {
            localIOException.printStackTrace();
        }
    }

    private void findFileName() {
        String str1 = find(this.entityMap.getProperty("dao_impl_p"), this.entityMap.getProperty("Entity") + "DaoImpl.java");
        this.daoImplFile = new File(str1);
        log.debug("daoImplFile:" + this.daoImplFile.getAbsolutePath());
        String str2 = find(this.entityMap.getProperty("dao_p"), this.entityMap.getProperty("Entity") + "Dao.java");
        this.daoFile = new File(str2);
        log.debug("daoFile:" + this.daoFile.getAbsolutePath());
        String str3 = find(this.entityMap.getProperty("manager_p"), this.entityMap.getProperty("Entity") + "Mng.java");
        this.managerFile = new File(str3);
        log.debug("managerFile:" + this.managerFile.getAbsolutePath());
        String str4 = find(this.entityMap.getProperty("manager_impl_p"), this.entityMap.getProperty("Entity") + "MngImpl.java");
        this.managerImplFile = new File(str4);
        log.debug("managerImplFile:" + this.managerImplFile.getAbsolutePath());
        String str5 = find(this.entityMap.getProperty("action_p"), this.entityMap.getProperty("Entity") + "Act.java");
        this.actionFile = new File(str5);
        log.debug("actionFile:" + this.actionFile.getAbsolutePath());
        String str6 = "WebContent/WEB-INF/" + this.entityMap.getProperty("config_sys") + "/" + this.entityMap.getProperty("config_entity") + "/";
        this.pageListFile = new File(str6 + "list.html");
        log.debug("pageListFile:" + this.pageListFile.getAbsolutePath());
        this.pageEditFile = new File(str6 + "edit.html");
        log.debug("pageEditFile:" + this.pageEditFile.getAbsolutePath());
        this.pageAddFile = new File(str6 + "add.html");
        log.debug("pageAddFile:" + this.pageAddFile.getAbsolutePath());
    }

    private void findTxt() {
        String str = this.entityMap.getProperty("template_dir");
        log.debug("tplPack:" + str);
        this.daoImpl = new File(find(str, "dao_impl.txt"));
        this.dao = new File(find(str, "dao.txt"));
        this.managerImpl = new File(find(str, "manager_impl.txt"));
        this.manager = new File(find(str, "manager.txt"));
        this.action = new File(find(str, "action.txt"));
        this.pageList = new File(find(str, "page_list.txt"));
        this.pageAdd = new File(find(str, "page_add.txt"));
        this.pageEdit = new File(find(str, "page_edit.txt"));
    }

    private void write() {
        try {
            if ("true".equals(this.entityMap.getProperty("is_dao"))) {
                writeFile(this.daoImplFile, findTemplate(this.daoImpl));
                writeFile(this.daoFile, findTemplate(this.dao));
            }
            if ("true".equals(this.entityMap.getProperty("is_manager"))) {
                writeFile(this.managerImplFile, findTemplate(this.managerImpl));
                writeFile(this.managerFile, findTemplate(this.manager));
            }
            if ("true".equals(this.entityMap.getProperty("is_action")))
                writeFile(this.actionFile, findTemplate(this.action));
            if ("true".equals(this.entityMap.getProperty("is_page"))) {
                writeFile(this.pageListFile, findTemplate(this.pageList));
                writeFile(this.pageAddFile, findTemplate(this.pageAdd));
                writeFile(this.pageEditFile, findTemplate(this.pageEdit));
            }
        } catch (IOException localIOException) {
            log.warn("write file faild! " + localIOException.getMessage());
        }
    }

    private String findTemplate(File paramFile) {
        String str1 = null;
        try {
            str1 = FileUtils.readFileToString(paramFile, "UTF-8");
            Set localSet = this.entityMap.keySet();
            Iterator localIterator = localSet.iterator();
            while (localIterator.hasNext()) {
                Object localObject = localIterator.next();
                String str2 = (String) localObject;
                String str3 = this.entityMap.getProperty(str2);
                str1 = str1.replaceAll("\\#\\{" + str2 + "\\}", str3);
            }
        } catch (IOException localIOException) {
            log.warn("read file faild. " + localIOException.getMessage());
        }
        return str1;
    }

    private String find(String paramString1, String paramString2) {
        log.debug("replace:" + paramString1);
        String str = paramString1.replaceAll("\\.", "/");
        log.debug("after relpace:" + str);
        return "src/" + str + "/" + paramString2;
    }

    public void generate() {
        init();
        findFileName();
        findTxt();
        write();
    }
}

